﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using RestSemitaService.Models;

namespace RestSemitaService.Controllers
{
    public class InvoiceDetailController : ApiController
    {
        // GET api/invoicedetail
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/invoicedetail/5
        public string Get(string invoice_no)
        {
            try
            {
                SqlConnection thisConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                thisConnection.Open();
                SqlCommand thisCommand = thisConnection.CreateCommand();
				InvoiceHead Invoice = new InvoiceHead();

				thisCommand.CommandText = @"SELECT	id,invoice_no,invoice_date,target_date,deliver_date
													,messenger_id,callcenter_id,invoice_status,address_text
													,address_gps,customer_id,customer_name, invoice_amount
											FROM	invoice_header
											Where	invoice_no=@invoice_no";
				thisCommand.Parameters.AddWithValue("@invoice_no", invoice_no);
				using (SqlDataReader reader = thisCommand.ExecuteReader())
				{
					if(reader.Read())
					{
						Invoice.InvoiceNo = reader["invoice_no"].ToString();
						Invoice.CustId = reader["customer_id"].ToString();
						Invoice.CustName = reader["customer_name"].ToString();
						Invoice.CustomerAddress = reader["address_text"].ToString();
						Invoice.DeliveryDate = reader["deliver_date"].ToString();
						Invoice.Amount = reader["invoice_amount"].ToString();
					}
				}

				thisCommand.CommandText = @"select	dt.invoice_no, dt.product_id, p.product_name, dt.unit, dt.net_amount, dt.unit_price,dt.qty
											from	invoice_detail dt join product_master p
													on dt.product_id = p.product_code 
											where	invoice_no = @invoice_no ";

				thisCommand.Parameters.Clear();
                thisCommand.Parameters.AddWithValue("@invoice_no", invoice_no);

                using (SqlDataReader reader = thisCommand.ExecuteReader())
                {
                    List<InvoiceDetail> invoiceDetails = new List<InvoiceDetail>();

					string invoiceNo;
					string productName;
					string unit;
					string netAmount;
					string pricePerUnit;
					string productId;
					string qty;


                    while (reader.Read())
                    {
						invoiceNo = reader["invoice_no"].ToString();
						productName = reader["product_name"].ToString();
						unit = reader["unit"].ToString();
						netAmount = reader["net_amount"].ToString();
						pricePerUnit = reader["unit_price"].ToString();
						productId = reader["product_id"].ToString();
						qty = reader["qty"].ToString();


						invoiceDetails.Add(new InvoiceDetail
                        {
                            InvoiceNo = invoiceNo,
                            ProductId = productId,
							ProductName = productName,
							Quantity = qty,
                            Unit = unit,
							NetAmount = netAmount,
							PricePerUnit = pricePerUnit
                        });
                    }
                    reader.Close();
                    thisConnection.Close();

					Invoice.InvoiceItems = invoiceDetails;

                    string json = JsonConvert.SerializeObject(new
                    {
						InvoiceHead = Invoice
                    });

                    return json;
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return e.StackTrace + " \n >>>>>>>>>>>>>>>>> \n" + e.Message;
            }
        }

        // POST api/invoicedetail
        public void Post([FromBody]string value)
        {
        }

        // PUT api/invoicedetail/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/invoicedetail/5
        public void Delete(int id)
        {
        }
    }
}
